import React from 'react'
// ===================================================================== Temp
import Table from '#cpt/temp/Table'
import Controls from '#cpt/temp/Controls'
import TableControls from '#cpt/temp/TableControls'
import Confirm from '#cpt/temp/Confirm'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== private component
const Box 		= $Lazy.load(()=>import('#tp/box/box'))
const AddModal 	= $Lazy.state(()=>import('./cpt/add-modal'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		selectedRows:[]
	}
	
	model = {}
	
	componentDidMount(){
		this.query = { kind_id: this.props.kind_id, kind_category:25001 }
		this.fetch()
	}
	
	// paging
	fetch = () => $fn.fetch.call(this,'de-rel-kind/subKind')
	
	// 表单
	cols = [
		{ title: '仪器编号', 	dataIndex: 'device_number', 	width:150 },
		{ title: '仪器名称', 	dataIndex: 'device_name', 		width:250 },
		{ title: '仪器型号', 	dataIndex: 'device_model', 		width:150 },
		{ title: '关联状态', 	dataIndex: 'enabled_name', 		width:80 },
		{ title: '生产产家', 	dataIndex: 'serial_man', 		width:200 },
		{ title: '使用科室', 	dataIndex: 'project_name', 		width:150 },
		{ title: '设备类型', 	dataIndex: 'device_type_name', 	width:100 },
		{ title: '启用日期', 	dataIndex: 'enabeld_date', 		...$fn.table.time },
		TableControls.controls.call(this,{ title:'是否默认', width: 80,  })
	]
	// 表格操作按钮
	table_controls = (text,{uuid, is_default}) => {
		const { disabled } = this.props
		return [
			TableControls.Switch.call(this, 'de-rel-kind/setdefault', { id: uuid }, { value:is_default, disValue:true, disabled }),
		]
	}
	// 操作按钮
	controls = () => {
		const { disabled } = this.props
		return [
			Controls.click.call(this,'添加', ()=> this.modelRef.open(this.props.kind_id), { disabled } ),
			Confirm.call(this,'删除', 'de-rel-kind/del', { isKeys: true, ghost:true }),
			Confirm.call(this,'仪器下线', 'de-rel-kind/off', { isKeys: true, ghost:true, clear:false }),
			Controls.push.call(this,'查看项目关联', ()=> {
				const { device_id, device_name, device_number, device_model, kind_id } = $fn.getSelectedRows.call(this)
				
				const hash = window.location.hash
				
				const url =`set-single-project/project-related?device_id=${device_id}&device_name=${$fn.encode(device_name)}&device_number=${device_number}&device_model=${device_model}&kind_id=${kind_id}`
				
				if(hash.indexOf('testing-center') !== -1){
					$fn.pushCenter(this,`/test-project/${url}`)
				}else if(hash.indexOf('system-settings') !== -1){
					$fn.pushRoot(this,`/system-settings/project-settings/test-project/${url}`)
				}
				
			}, { isKeys:true } ),
		]
	}
	render(){
		const { disabled } = this.props
		return (
			<>
				<Box title='仪器信息' controls={this.controls()} first contentClassName='pt10'>
					{/* 表格 */}
					{ Table.call(this, { wrapStyle: this.props.tableStyle, disabled, ...this.props }) }
				</Box>
				{/* 添加 */}
				<AddModal onRef={ref => this.modelRef = ref} onRefresh={this.fetch} />
			</>
		)
	}
}

export default Index